package org.hadoop.inverse;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import java.io.IOException;
public class InverseMR2 extends Configured implements Tool {
    @Override
    public int run(String[] args) throws Exception {
        Configuration conf = getConf();
        FileSystem fs = FileSystem.get(conf);
        Path dest = new Path("D:/a/out003");
        if (fs.exists(dest)) {
            fs.delete(dest, true);
        }
        Job job = Job.getInstance(conf, "InverseIndex2");
        job.setJarByClass(getClass());
        job.setMapperClass(IIMapper2.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        job.setReducerClass(IIReducer2.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        FileInputFormat.setInputPaths(job, new Path("D:/a/out002"));
        FileOutputFormat.setOutputPath(job, dest);
        int code = job.waitForCompletion(true) ? 0 : 1;
        return code;
    }
    public static class IIMapper2 extends Mapper<LongWritable, Text, Text, Text> {
        private Text key = new Text();
        private Text value = new Text();
        @Override
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] strs = value.toString().split("\\s+");
            this.key.set(strs[0]);//Hello
            this.value.set(strs[1] + "\t" + strs[2]);//a.txt,1
            context.write(this.key, this.value);
        }
    }
    public static class IIReducer2 extends Reducer<Text, Text, Text, LongWritable> {
        private LongWritable sum = new LongWritable(0L);
        @Override
        public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            this.sum.set(0L);
            String str = "";
            for (Text t : values) {
                String[] strs = t.toString().split("\t");
                this.sum.set(this.sum.get() + Long.parseLong(strs[1]));
                str += "\t" + t.toString();
            }
            key.set(key.toString() + "\t" + str);
            context.write(key, this.sum);
        }
    }
    public static void main(String[] args) throws Exception {
        int code = ToolRunner.run(new InverseMR2(), args);
        System.exit(code);
    }
}
